package com.mt.mtyx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.mtyx.entity.base.BaseEntity;
import com.mt.mtyx.entity.product.Category;
import com.mt.mtyx.product.mapper.CategoryMapper;
import com.mt.mtyx.product.service.CategoryService;
import com.mt.mtyx.vo.product.CategoryQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品分类业务层
 *
 * @author WMT
 * @Date 2023/7/20 10:38
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Override
    public IPage<Category> selectAll(Page<Category> categoryPage, CategoryQueryVo queryVo) {
        String name = queryVo.getName();
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like(Category::getName, name);
        }
        return baseMapper.selectPage(categoryPage, wrapper);
    }

    @Override
    public Category getDetails(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Long countByName(String name) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getName, name);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public boolean existsByName(String categoryName) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getName, categoryName);
        return baseMapper.exists(wrapper);
    }

    @Override
    public int insert(Category category) {
        LocalDateTime now = LocalDateTime.now();
        category.setUpdateTime(now);
        category.setCreateTime(now);
        category.setIsDeleted(BaseEntity.NOT_DELETED);
        return baseMapper.insert(category);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int batchDelete(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    @Override
    public int update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(category);
    }

    @Override
    public List<Category> findAllList() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Category> getCategoryListByIds(List<Long> ids) {
        return baseMapper.selectBatchIds(ids);
    }
}
